<?php
/**
 * In this File the class '\UF\IO\MimetypeTool' is defined.
 *
 * @category   UniKap-Framework
 * @package    IO
 * @subpackage Core
 * @author     Ulf -UniKado- Kadner <ulfikado@gmail.com>
 * @since      2014-03-29 16:17
 * @version    0.1
 */

namespace UF\IO
{

    /**
      * Some static Methods for mime types.
      *
      * @since  v0.1
      */
    class MimetypeTool
    {

        # <editor-fold defaultstate="collapsed" desc="- - - >   H I D D E N   C O N S T R U C T O R   - - - - - - - - - - - - - - - - -">

        /**
         * @since  v0.1
         */
        private function __construct() { }

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - -   P R I V A T E   S T A T I C   F I E L D S   - - - - - - - - - - - - - -">

        /**
         * @since  v0.1
         */
        private static $extensionToMimetype = [
            'afl' => 'video/animaflex',
            'ai' => 'application/postscript',
            'aif' => 'audio/x-aiff',
            'aim' => 'application/x-aim',
            'arc' => 'application/octet-stream',
            'arj' => 'application/octet-stream',
            'asf' => 'video/x-ms-asf',
            'asm' => 'text/x-asm',
            'asp' => 'text/asp',
            'au' => 'audio/basic',
            'avi' => 'video/avi',
            'avs' => 'video/avs-video',
            'bin' => 'application/octet-stream',
            'bmp' => 'image/bmp',
            'bsh' => 'application/x-bsh',
            'bz' => 'application/x-bzip',
            'bz2' => 'application/x-bzip2',
            'c' => 'text/x-c',
            'c++' => 'text/plain',
            'cat' => 'application/vnd.ms-pki.seccat',
            'cc' => 'text/plain',
            'cer' => 'application/x-x509-ca-cert',
            'class' => 'application/java',
            'conf' => 'text/plain',
            'cpio' => 'application/x-cpio',
            'cpp' => 'text/x-c',
            'csh' => 'application/x-csh',
            'css' => 'text/css',
            'def' => 'text/plain',
            'der' => 'application/x-x509-ca-cert',
            'doc' => 'application/msword',
            'dot' => 'application/msword',
            'dump' => 'application/octet-stream',
            'dvi' => 'application/x-dvi',
            'dwg' => 'application/acad',
            'el' => 'text/x-script.elisp',
            'eps' => 'application/postscript',
            'etx' => 'text/x-setext',
            'exe' => 'application/octet-stream',
            'fdf' => 'application/vnd.fdf',
            'fli' => 'video/x-fli',
            'fpx' => 'image/vnd.fpx',
            'gif' => 'image/gif',
            'gtar' => 'application/x-gtar',
            'gz' => 'application/x-gzip',
            'gzip' => 'application/x-gzip',
            'hdf' => 'application/x-hdf',
            'help' => 'application/x-helpfile',
            'hlp' => 'application/x-winhelp',
            'hqx' => 'application/mac-binhex',
            'hta' => 'application/hta',
            'htm' => 'text/html',
            'html' => 'text/html',
            'htmls' => 'text/html',
            'htx' => 'text/html',
            'ico' => 'image/x-icon',
            'imap' => 'application/x-httpd-imap',
            'inf' => 'application/inf',
            'java' => 'text/x-java-source',
            'jfif' => 'image/jpeg',
            'jfif' => 'image/pjpeg',
            'jpe' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'jpg' => 'image/jpeg',
            'js' => 'application/x-javascript',
            'kar' => 'music/x-karaoke',
            'ksh' => 'application/x-ksh',
            'latex' => 'application/x-latex',
            'lha' => 'application/octet-stream',
            'list' => 'text/plain',
            'log' => 'text/plain',
            'lsp' => 'application/x-lisp',
            'lst' => 'text/plain',
            'ltx' => 'application/x-latex',
            'lzh' => 'application/octet-stream',
            'm1v' => 'video/mpeg',
            'm2a' => 'audio/mpeg',
            'm2v' => 'video/mpeg',
            'm3u' => 'audio/x-mpequrl',
            'man' => 'application/x-troff-man',
            'mcd' => 'application/mcad',
            'mht' => 'message/rfc822',
            'mhtml' => 'message/rfc822',
            'mid' => 'application/x-midi',
            'midi' => 'application/x-midi',
            'mime' => 'message/rfc822',
            'mm' => 'application/base64',
            'mod' => 'audio/x-mod',
            'moov' => 'video/quicktime',
            'mov' => 'video/quicktime',
            'mp2' => 'audio/x-mpeg',
            'mp3' => 'audio/mpeg3',
            'mpe' => 'video/mpeg',
            'mpeg' => 'video/mpeg',
            'mpg' => 'video/mpeg',
            'mpga' => 'audio/mpeg',
            'mpp' => 'application/vnd.ms-project',
            'mv' => 'video/x-sgi-movie',
            'pas' => 'text/pascal',
            'pbm' => 'image/x-portable-bitmap',
            'pct' => 'image/x-pict',
            'pcx' => 'image/x-pcx',
            'pdf' => 'application/pdf',
            'pgm' => 'image/x-portable-graymap',
            'php' => 'text/plain',
            'pic' => 'image/pict',
            'pict' => 'image/pict',
            'pl' => 'text/plain',
            'png' => 'image/png',
            'pot' => 'application/vnd.ms-powerpoint',
            'ppa' => 'application/vnd.ms-powerpoint',
            'pps' => 'application/vnd.ms-powerpoint',
            'ppt' => 'application/vnd.ms-powerpoint',
            'ps' => 'application/postscript',
            'py' => 'text/x-script.phyton',
            'qt' => 'video/quicktime',
            'ra' => 'audio/x-realaudio',
            'ram' => 'audio/x-pn-realaudio',
            'rm' => 'application/vnd.rn-realmedia',
            'rt' => 'text/richtext',
            'rtf' => 'application/x-rtf',
            'rtx' => 'text/richtext',
            'rv' => 'video/vnd.rn-realvideo',
            'sgm' => 'text/x-sgml',
            'sgml' => 'text/x-sgml',
            'sh' => 'application/x-sh',
            'shtml' => 'text/html',
            'snd' => 'audio/basic',
            'swf' => 'application/x-shockwave-flash',
            'tar' => 'application/x-tar',
            'tcl' => 'application/x-tcl',
            'tcsh' => 'text/x-script.tcsh',
            'tex' => 'application/x-tex',
            'texi' => 'application/x-texinfo',
            'texinfo' => 'application/x-texinfo',
            'text' => 'text/plain',
            'tgz' => 'application/x-compressed',
            'tif' => 'image/tiff',
            'tiff' => 'image/tiff',
            'txt' => 'text/plain',
            'vcs' => 'text/x-vcalendar',
            'voc' => 'audio/voc',
            'vrml' => 'application/x-vrml',
            'wav' => 'audio/x-wav',
            'wbmp' => 'image/vnd.wap.wbmp',
            'wml' => 'text/vnd.wap.wml',
            'word' => 'application/msword',
            'xbm' => 'image/x-xbitmap',
            'xla' => 'application/x-msexcel',
            'xlb' => 'application/vnd.ms-excel',
            'xlc' => 'application/vnd.ms-excel',
            'xls' => 'application/vnd.ms-excel',
            'xm' => 'audio/xm',
            'xml' => 'text/xml',
            'xpm' => 'image/xpm',
            'x-png' => 'image/png',
            'zip' => 'application/x-zip-compressed',
            'zsh' => 'text/x-script.zsh',
        ];

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - -   P U B L I C   S T A T I C   M E T H O D S   - - - - - - - - - - - - - -">

        /**
         * Returns the mimetype of defined file name extension.
         *
         * @param  string $extension File name extension (with or without the leading dot)
         * @return string Mimetype
         * @since  v0.1
         */
        public static function GetByExtension( $extension )
        {
            $lowerExt = \strtolower( ltrim( $extension, '.' ) );
            if ( isset(self::$extensionToMimetype[$lowerExt])) return self::$extensionToMimetype[$lowerExt];
            return 'application/octet-stream';
        }

        /**
         * Returns the mimetype of defined file name.
         *
         * @param  string $fileName The file name or path
         * @return string Mimetype
         * @since  v0.1
         */
        public static function GetByFileName( $fileName )
        {
            return self::GetByExtension( \UF\IO\File::GetExtension( $fileName ) );
        }

        /**
         * Returns if the defined mimetype is a image type.
         *
         * @param  string $mimetype
         * @return bool
         * @since  v0.1
         */
        public static function IsImageType( $mimetype )
        {
            return \UF\str_startswith( $mimetype, 'image/' );
        }

        /**
         * Returns if the defined file name extension is usable for images.
         *
         * @param  string $extension File name extension (with or without the leading dot)
         * @return bool
         * @since  v0.1
         */
        public static function IsImageTypeExtension( $extension )
        {
            return \UF\str_startswithh( self::GetByExtension($extension), 'image/' );
        }

        /**
         * Returns if the defined file name or path is usable for images.
         *
         * @param  string $fileName The file name or path
         * @return bool
         * @since  v0.1
         */
        public static function IsImageTypeFileName( $fileName )
        {
            return \UF\str_startswith( self::GetByFileName($fileName), 'image/' );
        }

        /**
         * Returns if the defined file contains a valid web image format.
         *
         * @param  string $file The file to check
         * @return bool
         * @since  v0.1
         */
        public static function IsValidImageFile( $file )
        {
            return (bool) \getimagesize($file);
        }

        # </editor-fold>

    }

}

